package com.code.generate;

import java.util.Map;

import com.code.generate.enums.ControlType;

/**
 * 字段、属性信息
 * 
 * @author Administrator
 * 
 */
public class Field {

	/**
	 * 数据库字段名
	 */
	protected String tableField;

	/**
	 * 数据库的字段类型
	 */
	protected String dataType;

	/**
	 * 对象字段名
	 */
	protected String fieldName;
	protected String fieldCName;

	/**
	 * 对象字段类型
	 * 
	 */
	protected Object fieldType;

	/**
	 * 页面输入控件
	 */
	protected ControlType inputType;
	/**
	 * 是否查询
	 */
	protected boolean whetherQuery = false;
	/**
	 * 是否列表
	 */
	protected boolean whetherList = true;
	/**
	 * 是否可编辑
	 */
	protected boolean whetherEdit = true;
	/**
	 * 是否主键
	 */
	protected boolean whetherPrimaryKey = false;
	/**
	 * 是否要求的
	 */
	protected boolean whetherRequest = false;
	protected String remark;
	/**
	 * 是否唯一
	 */
	protected boolean isUnique = false;
	/**
	 * 字段长度
	 */
	protected int length;

	public Field() {
	}

	public Field(Map<String, Object> map) {
		this.tableField = ("" + map.get("NAME")).trim();
		this.dataType = ("" + map.get("COLTYPE")).trim();
		this.fieldName = CommonUtils.getProperty(("" + map.get("NAME")).trim());
		this.fieldType = CommonUtils.getFieldType(("" + map.get("COLTYPE")).trim(), this.tableField);
		if (tableField.equalsIgnoreCase("AUDIT_STATE")) {
			this.fieldType = "AuditState";
			this.fieldName = "auditState";
		}
		if ("N".equals(("" + map.get("NULLS")).trim())) {
			this.whetherRequest = true;
		}
		if ("1".equals(("" + map.get("KEYSEQ")).trim())) {
			this.whetherPrimaryKey = true;
			this.whetherEdit = false;
			this.whetherList = false;
			this.isUnique = true;
		}
		if ("String".equals(fieldType)) {
			try {
				this.length = new Integer(("" + map.get("LENGTH")).trim()).intValue();
			} catch (java.lang.NumberFormatException e) {
				this.length = 0;
			}

		}
		this.remark = ("" + map.get("REMARKS")).trim();
		if ("String".equals(fieldType)) {
			if (length >= 100) {
				inputType = ControlType.TEXTAREA;
			} else {
				inputType = ControlType.TEXT;
			}
		} else if ("Timestamp".equals(dataType)) {
			inputType = ControlType.DATE_TIME;
		} else if ("Date".equals(dataType)) {
			inputType = ControlType.DATE;
		} else if ("Time".equals(dataType)) {
			inputType = ControlType.TIME;
		} else if ("Boolean".equals(dataType)) {
			inputType = ControlType.STATIC_RADIO;
		}
	}

	public String getTableField() {
		return tableField;
	}

	public void setTableField(String tableField) {
		this.tableField = tableField;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldCName() {
		return fieldCName;
	}

	public void setFieldCName(String fieldCName) {
		this.fieldCName = fieldCName;
	}

	public Object getFieldType() {
		return fieldType;
	}

	public void setFieldType(Object fieldType) {
		this.fieldType = fieldType;
	}

	public ControlType getInputType() {
		return inputType;
	}

	public void setInputType(ControlType inputType) {
		this.inputType = inputType;
	}

	public boolean isWhetherQuery() {
		return whetherQuery;
	}

	public void setWhetherQuery(boolean whetherQuery) {
		this.whetherQuery = whetherQuery;
	}

	public boolean isWhetherList() {
		return whetherList;
	}

	public void setWhetherList(boolean whetherList) {
		this.whetherList = whetherList;
	}

	public boolean isWhetherEdit() {
		return whetherEdit;
	}

	public void setWhetherEdit(boolean whetherEdit) {
		this.whetherEdit = whetherEdit;
	}

	public boolean isWhetherPrimaryKey() {
		return whetherPrimaryKey;
	}

	public void setWhetherPrimaryKey(boolean whetherPrimaryKey) {
		this.whetherPrimaryKey = whetherPrimaryKey;
	}

	public boolean isWhetherRequest() {
		return whetherRequest;
	}

	public void setWhetherRequest(boolean whetherRequest) {
		this.whetherRequest = whetherRequest;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public boolean isUnique() {
		return isUnique;
	}

	public void setUnique(boolean isUnique) {
		this.isUnique = isUnique;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(" |dataType:");
		sb.append(this.dataType);
		sb.append(" |fieldCName:");
		sb.append(this.fieldCName);
		sb.append(" |fieldName:");
		sb.append(this.fieldName);
		sb.append(" |fieldType:");
		sb.append(this.fieldType);
		sb.append(" |inputType:");
		sb.append(this.inputType);
		sb.append(" |isUnique:");
		sb.append(this.isUnique);
		sb.append(" |length:");
		sb.append(this.length);
		sb.append(" |remark:");
		sb.append(this.remark);
		sb.append(" |tableField:");
		sb.append(this.tableField);
		sb.append(" |whetherEdit:");
		sb.append(this.whetherEdit);
		sb.append(" |whetherList:");
		sb.append(this.whetherList);
		sb.append(" |whetherPrimaryKey:");
		sb.append(this.whetherPrimaryKey);
		sb.append(" |whetherQuery:");
		sb.append(this.whetherQuery);
		sb.append(" |whetherRequest:");
		sb.append(this.whetherRequest);
		return sb.toString();
	}
}
